﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructDemo.LikedListDemo
{
    public class EndInsertWay<T>
    {
        private class Node
        {
            private T _data;
            public T Data
            {
                get { return _data; }
                set { _data = value; }
            }

            private Node _nextNode;
            public Node NextNode
            {
                get { return _nextNode; }
                set { _nextNode = value; }
            }

            public Node(T t)
            {
                _nextNode = null;
                Data = t;
            }
        }

        private Node _nodehead;
        private Node _end;

        public EndInsertWay()
        {
            _nodehead = null;
            _end = null;
        }

        public void AddEnd(T t)
        {
            var node = new Node(t);
            if (_nodehead == null)
            {
                _nodehead = node;
                _end = node;
            }
            else
            {

                _end.NextNode = node;
                _end = node;
            }
        }

        public IEnumerable<T> GetEnumerable()
        {
            Node current = _nodehead;
            while (current != null)
            {
                yield return current.Data;
                current = current.NextNode;
            }
        }
    }
}
